package com.smallyu.order.controller;

import com.smallyu.order.bean.User;
import com.smallyu.order.core.YuResult;
import com.smallyu.order.service.YuLoginService;
import com.smallyu.order.utils.YuJwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class YuLoginController {

    @Autowired
    private YuLoginService yuLoginService;

    @Autowired
    private YuJwtUtils yuJwtUtils;

    @GetMapping("/login")
    public YuResult doLogin(User user, HttpServletRequest request, HttpServletResponse response){
        boolean state = yuLoginService.checkUser(user);
        if (state){
            String token = yuJwtUtils.createToken(user.getUsername(), user.getPassword());
            response.setHeader("yuauth",token);
            return YuResult.success();
        }
        return YuResult.error();
    }

    @GetMapping("/test")
    public YuResult test(){
        return YuResult.success("测试成功");
    }
}
